package com.dmm.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmm.redis.redisService;
import com.dmm.result.Result;
import com.dmm.user.entity.Apply;
import com.dmm.user.entity.Question;
import com.dmm.user.service.QuestionService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com/dmm
 * @since 2023-04-06
 */
@RestController
@CrossOrigin
@RequestMapping("/user/question")
public class QuestionController {

    @Autowired
    QuestionService questionService;

    @Autowired
    redisService redisService;


    @ApiOperation("题库添加")
    @PutMapping("/Addquestions")
    public Result additive(@RequestBody Question question){

        boolean save = questionService.save(question);

        return Result.success(null);

    }



    @ApiOperation("分页查询")
    @PostMapping("{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit
            ) {
        //创建page对象，传递当前页和每页记录数
        Page<Question> pageParam = new Page<>(page, limit);


        IPage<Question> pageModel = questionService.page(pageParam);
        //调用方法得到分页查询结果

        return Result.success(pageModel);
    }


    @ApiOperation("逻辑删除")
    @DeleteMapping("/deleteId/{id}")
    public Result deleteId(@PathVariable Long id){

        boolean b = questionService.removeById(id);
        if(b){
            return Result.success(null);
        }
        return Result.fail(null);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/Batch_delete")
    public Result Batch_delete(@RequestBody List<Long> list){


        boolean b = questionService.removeByIds(list);

        return b? Result.success(null):Result.fail(null);
    }


    @ApiOperation("根据id查题目")
    @GetMapping("/getbyid/{id}")
    public Result getbyid(@PathVariable Long id){

        System.out.println(id);

        Question byId = questionService.getById(id);


        return Result.success(byId);
    }

    @ApiOperation("根据id修改题目")
    @PutMapping("/update/{id}")
    public Result updateid(@PathVariable Integer id,
                           @RequestBody Question question){

        System.out.println(id);
            QueryWrapper<Question> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("id",id);
            question.setId(id);
        questionService.update(question,queryWrapper);

        return Result.success(null);
    }

    @ApiOperation("获取题目")
    @GetMapping("/getsubject/{email}")
    public Result getsubject(@PathVariable String email){


       List<Question> list=questionService.list();

       redisService.set(email+"question",list,20);
        System.out.println(list);
        return Result.success(list);
    }


}

